# -*- coding: utf-8 -*-

import math
import re
import time
from scrapy import log
from snowball_crawler.items import  SnowballStockbarItem
from snowball_crawler.pipelines import mongodb


class snowballStockbarPipeline(mongodb.mongodbPipeline):
    """
       save stock bar data
    """
    pattern = re.compile(r'[\w]{3}\s[\w]{3}\s[\d]{1,2}\s\d+:\d+:\d+\s\+([\d]{4})\s[\d]{4}')
    def parse_time(self, src):       #有些2000年之前的行情返回的是 +0900 需要用正则表达式匹配
        match = self.pattern.match(src)
        if match:
            time_format = '%a %b %d %H:%M:%S +'+match.group(1)+' %Y'  # 源格式如： Fri May 23 00:00:00 +0800 2003
            return time.strptime(src, time_format)

    def process_item(self, item, spider):
         if isinstance(item, SnowballStockbarItem):
             bar_data = item['bardata']
             bar_type = item['bartype']
             symbol = item['symbol']
             updated_date = time.strftime("%Y-%m-%d", time.localtime())
             info = {
                 'symbol': symbol,
                 'last_update_date': updated_date
             }

             for bar in bar_data:
                 bartime = self.parse_time(bar['time'])
                 date = time.strftime("%Y-%m-%d", bartime)
                 bar['time'] = time.strftime("%Y-%m-%d %H %M %S", bartime)
                 info.update({'bar_data.%s.%s' % (bar_type,date): bar})

             result = self.db['stock_bar'].update_one({'symbol': symbol}, {'$set': info}, True)
             log.msg("stock bar %s updated to MongoDB database %s" % (result, self.MONGODB_DB), level=log.DEBUG,spider=spider)

         return item
